package satirGame;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.awt.*;
import java.awt.event.*;

public class Player extends JDialog{
	/**Constructor return JDialog for Player information setting
	 * 
	 */
	private static final long serialVersionUID = 1194108133407586893L;
	private String name;
	private static String NameText = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	private static String SymbolsText = "!@#$%^&*()";//change to static in 16/3/2012
	private int RealRole; // 0-5 Role ID
	private int GameRole;
	private int score=0;
	private int step=1;
	private int playerID;
	private static int panelWidth=100, panelHeight=100;
	static int nameMaxLength = 10; //check name Max length
	private int curPos;
	private static ArrayList<String> roleString=new ArrayList<String>(Arrays.asList("GrandPa","GrandMa","Father","Mother","Son","Daughter"));
	private ArrayList<Integer> tool_ID;
	public JPanel p; 
	public JLabel scoreLable;
	public Player(){
	}
	public Player(int[] remainRole, final JFrame parent,int id){
		super(parent,true);
		JPanel p = new JPanel(new FlowLayout());
		getContentPane().add(p);
		JLabel nLabel = new JLabel("Input Player Name: ");
		p.add(nLabel);
		final JTextField inputName = new JTextField(20);
		nLabel.setLabelFor(inputName);
		p.add(inputName);
		JLabel rLabel = new JLabel("Your Role in Real Life: ");
		p.add(rLabel);
		final ButtonGroup roleGroup = new ButtonGroup();
		for(int i=0;i<6;i++){
			if(remainRole[i]==(-1)){
				JRadioButton roleBtn=new JRadioButton(roleString.get(i));
				roleBtn.setActionCommand(roleString.get(i));
				roleGroup.add(roleBtn);
				p.add(roleBtn);
			}
		}
		JButton okButton = new JButton("OK");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String strTemp=inputName.getText();
				// TODO Auto-generated method stub
				if(roleGroup.getSelection()==null){
					JOptionPane.showMessageDialog(parent, "Please Select a role");
				}else if (strTemp.length()==0) {
					JOptionPane.showMessageDialog(parent, "Please Entry Name");
				}else {
					setName(strTemp);
					//String roleSelected=roleGroup.getSelection().getActionCommand();
					int roleNum=roleString.indexOf(roleGroup.getSelection().getActionCommand());
					setRealRole(roleNum);
					dispose();
				}
				
			}
		});
	    p.add(okButton); 
		pack();
        setLocationRelativeTo(parent);
        setVisible(true);
		playerID=id;
		curPos=(id-1)*10;
	}
	
	public void setName(String username){
		name = username;
	}
	public String getName(){
		return name;
	}
	
	public int getRealRole(){
		return RealRole;
	}
	public void setRealRole(int role_id){
		if (role_id>=0 && role_id<=5){
			RealRole = role_id;
		}
	}
	public int getRole(){
		return GameRole;
	}
	
	public void setRole(int role_id){
		if (role_id>=0 && role_id<=5){
			GameRole = role_id;
		}
	}
	
	public void addScore(int scoreGet){
		score += scoreGet;
		scoreLable.setText(String.valueOf(getScore()));
		scoreLable.revalidate();
	}
	public int getScore(){
		return score;
	}
	
	public void setID(int id){
		playerID=id;
	}
	public int getID(){
		return playerID;
	}
	
	public void setStep(int sNum){
		step+=sNum;
	}
	
	public int getStep(){
		return step;
	}
	
	public boolean isNullName(){
		
		// case "123", {false}
		// case " " , {true}
		if (name.length() == 0){
			return true;
		}else
			return false;
	}
	
	public boolean isContainSymbols(){
		if (SymbolsText.contains(name)){
			return true;
		}else
			return false;
	}
	
	public boolean isContainChar(){
		
		// case "123", {true}
		// case " " , {false}
		// case "@", {false}
		// Cannot enter same char more then one time, as need add for loop for checking
		if (NameText.contains(name)){
			return true;
		}else
			return false;
	}
	
	public boolean isnameMaxLengthOverflow(){ //check nameMaxLength overflow
		if (name.length()> 10){
			return true;
		}else
			return false;
	}

	public void setCurPos(int curPos) {
		this.curPos = curPos;
	}

	public int getCurPos() {
		return curPos;
	}

	public static void setPanelWidth(int panelWidth) {
		Player.panelWidth = panelWidth;
	}

	public static int getPanelWidth() {
		return panelWidth;
	}

	public static void setPanelHeight(int panelHeight) {
		Player.panelHeight = panelHeight;
	}

	public static int getPanelHeight() {
		return panelHeight;
	}
	public void setInfoPanelAsCurrent(){
		p.setBackground(Color.yellow);
		p.revalidate();
	}
	public void setInfoPanelAsPrev(){
		p.setBackground(Color.lightGray);
		p.revalidate();
	}
	
	public JPanel PlayerInfoPanel(){
		GridLayout gridLayout=new GridLayout(3,2,5,5);
		
		p= new JPanel(gridLayout);
		JLabel pLabel=new JLabel();
		pLabel.setText("Player "+ String.valueOf(playerID));
		p.add(pLabel);
		p.add(new JLabel("------"));
		JLabel nameLable = new JLabel();
		nameLable.setText(getName());
		 p.add(new JLabel("Name:"));
		 p.add(nameLable);
	      //p.add(new JLabel("Role:"));
	      p.add(new JLabel("Self-Esteem: "));
	      scoreLable = new JLabel();
	      scoreLable.setText(String.valueOf(getScore()));
	      p.add(scoreLable);
		 return p;
	}
	
}


/*----------------Version detail----------------------------

Modified time: 02:37_05/03/2012

Editor: Lam Kit Yung

Content: 

Add variables:

Add new functions

----------------Version detail----------------------------
Modified time: 13:37_08/04/2012

Editor: Lam Kit Yung

Content: 

Add new functions
public JDialog setPlayer();
public JPanel PlayerInfoPanel();

Change class type
implements ActionListener;
change Constructor Player
-----------------------------------------------------------
----------------Version detail----------------------------
Modified time: 17:20_09/04/2012

Editor: Lam Kit Yung

Content: 

Delete functions
setPlayer();

Modify Function:
public JPanel PlayerInfoPanel(int xpos,int ypos);

Change class type
change Constructor Player
-----------------------------------------------------------
----------------Version detail----------------------------
Modified time: 21:20_11/04/2012

Editor: Lam Kit Yung

Content: 

Modify Function:
public JPanel PlayerInfoPanel(int xpos,int ypos);

-----------------------------------------------------------
*/
